#include "CheckInRequestPanel.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QAction>
#include <QGridLayout>
#include <QHBoxLayout>


#include "../../../../../mes/utils/TaskManager.h"
#include "../../../../models/CheckInData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"

QHash<QString,QLineEdit*> checkInRequestLineList;

CheckInRequestPanel::CheckInRequestPanel(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
}

void CheckInRequestPanel::initWidgets()
{
    layout = new QGridLayout();
    const auto btnExecute = new QPushButton(tr("执行请求"));
    btnExecute->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    layout->addWidget(buildParamPanel(),0,0,1,1);
    layout->addWidget(btnExecute,1,0,1,1);

    setLayout(layout);
    connect(btnExecute,&QPushButton::clicked,this,&CheckInRequestPanel::execute);
}

void CheckInRequestPanel::setupUi()
{
    initWidgets();
    // execute();
}

QWidget *CheckInRequestPanel::buildParamPanel()
{
    const auto widget = new QWidget();
    const auto lbLocationNo = new QLabel(tr("工位号"));
    const auto txtLocationNo = new QLineEdit();
    txtLocationNo->setPlaceholderText(tr("输入工位号"));
    const auto lbPcu = new QLabel(tr("条码"));
    const auto txtPcu = new QLineEdit();
    txtPcu->setPlaceholderText(tr("输入条码"));
    const auto lbBatch = new QLabel(tr("工段"));
    const auto txtBatch = new QLineEdit();
    txtBatch->setPlaceholderText(tr("输入工段号"));


    const auto paramLayout = new QGridLayout();
    paramLayout->addWidget(lbLocationNo,0,0,1,1);
    paramLayout->addWidget(txtLocationNo,0,1,1,1);
    paramLayout->addWidget(lbPcu,2,0,1,1);
    paramLayout->addWidget(txtPcu,2,1,1,1);
    paramLayout->addWidget(lbBatch,3,0,1,1);
    paramLayout->addWidget(txtBatch,3,1,1,1);
    widget->setLayout(paramLayout);

    checkInRequestLineList.insert("locationNo",txtLocationNo);
    checkInRequestLineList.insert("pcu",txtPcu);
    checkInRequestLineList.insert("batch",txtBatch);

    return widget;
}

void CheckInRequestPanel::execute()
{
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);
    std::string locationNo = checkInRequestLineList["locationNo"]->text().toStdString();
    std::string batch = checkInRequestLineList["batch"]->text().toStdString();
    std::string pcu = checkInRequestLineList["pcu"]->text().toStdString();


    BaseData<CheckInData>::execute(&CheckInData::setLocationNo,locationNo);
    BaseData<CheckInData>::execute(&CheckInData::setBatch,batch);
    BaseData<CheckInData>::execute(&CheckInData::setPCU,pcu);
    BaseData<CommonData>::execute(&CommonData::setEventTime,dateTime.toStdString());
    BaseData<CommonData>::execute(&CommonData::setTransId,transIdStr.toStdString());

    // QString factoryNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo));
    // QString machineNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));
    // QString eventUser = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser));

    // QList<BatchCheckInPcuInfo> pcuInfo;
    // BatchCheckInPcuInfo item {QString::fromStdString(locationNo),QString::fromStdString(pcu)};
    // pcuInfo.append(item);

    // TaskManager task(BaseData<CommonData>::execute(&CommonData::getNetwork));
    // const auto response = task.BatchCheckIn(factoryNo,eventUser,machineNo,QString::fromStdString(batch),pcuInfo);

    emit SignalBus::instance()->sendCheckin();


}


